<?php
if(isset($_SESSION['QuyenHan']) && $_SESSION['QuyenHan'] == 1)
{
	$vanbanden = new vanbandenbs();
	
	if(isset($_POST['btnTim']))
	{
		
		$SovaKyHieu = $_POST['SovaKyHieu'];
		$NoiBanHanh = $_POST['NoiBanHanh'];
		$NgayBanHanh = str_replace('-',',',$_POST['NgayBanHanh']);
		$NgayHetHan = $_POST['TrichYeu'];
		
		
		$vanbanden->SovaKyHieu = $SovaKyHieu;
		$vanbanden->NgayBanHanh = $NgayBanHanh;
		$vanbanden->TrichYeu = $TrichYeu;
		$vanbanden->NoiBanHanh = $NoiBanHanh;
	
	}
	if(isset($_GET['ds'])&&$_GET['ds'] == 1)
	{
		  $vanbanden->MaTrangThai = 2; 
		  $mtt=1;
	}
	else
		 $vanbanden->MaTrangThai = 1; 
	
	$vanbanden->MaNguoiDuyet = $_SESSION['MaNguoiDung']; 
	$dsvb = $vanbanden->GetTrangThaiVanBan();
	
	$tongsodong =  count($dsvb);
	
	if(!isset($_GET['start']))
		$start = 0;
	else
		$start = $_GET['start'];
	
	$limit = 15;
	$next = $start + $limit;
	$back = $start - $limit;
	$danhsach = $vanbanden->pageTrangThaiVanBan($start, $limit);
	
	
	$phongban = new phongbanbs();
	$dsphongban = $phongban->GetList();
	
?>

<div id="NoiDung" style="position:relative" class="form">
<h3  class="text-primary">VĂN BẢN ĐẾN</h3>
<div style="position:absolute; left:840px; top:10px;"><a href="#"  style="margin-left:10px"><span id="clickSearch" class="glyphicon glyphicon-search" style="font-size:18px"></span></a></div>
<div style="margin-top:20px; display:none" id="search">
  <form  action="vanbanden-duyet" method="post" role="form">
    <table class="table borderless">
      <tbody>
        <tr>
          <td><div class="form-group form-custom">
              <label  for="sovakyhieu">Số và ký hiệu:</label>
              <input type="text" class="form-control" placeholder="Số và ký hiệu" name="SovaKyHieu" class="required" />
            </div></td>
          <td><div class="form-group form-custom">
              <label  for="noibanhanh">Nơi ban hành:</label>
              <input type="text" class="form-control" placeholder="Nơi ban hành" name="NoiBanHanh" class="required" />
            </div></td>
        </tr>
        <tr>
          <td><div class=" form-group form-custom control-group">
              <label  for="trichyeu">Trích yếu</label>
              <input type="text" class="form-control" placeholder="Trích yếu" name="TrichYeu" class="required" />
            </div></td>
          <td><div class=" form-group form-custom control-group">
              <label class="control-label">Ngày ban hành</label>
              <div class="controls input-append date form_date" data-date="" data-date-format="dd MM yyyy" data-link-field="dtp_input2" data-link-format="yyyy-mm-dd">
                <input size="16" type="text" value="" readonly="readonly" name="NgayBanHanh" class="form-control">
                <span class="add-on"><i class="icon-remove"></i></span> <span class="add-on"><i class="icon-th"></i></span> </div>
            </div></td>
        </tr>
        <tr>
          <td colspan="2"><input type="submit" value="Tìm" class="btn btn-primary btn-primary pull-right" name="btnTim" /></td>
        </tr>
      </tbody>
    </table>
  </form>
</div>
<script>
 $("#clickSearch").click(function()
	{
		$("#search").toggle("fast");
})

 </script>
<div class="btnXemLaiVanBan">
  <?php
        	if(isset($_GET['ds'])&&$_GET['ds'] == 1)
			{
		?>
  <a href="vanbanden-duyet_0" >
  <label class= 'label label-danger'>Trở lại</label>
  </a>
  <?php
			}else
			{?>
  <a href="vanbanden-duyet_1" >
  <label class= 'label label-danger'>Văn bản đã xử lý</label>
  </a>
  <?php }?>
</div>
<!--Bang van ban den-->

<div style="margin-top:20px;">
<form action="vanbanden-duyet" method="post">
<table class="table table-hover table-bordered table-striped">
  <thead>
    <tr>
      <th style="text-align:center"> STT </th>
      <th  style="text-align:center"> Số và ký hiệu </th>
      <th style="text-align:center"> Ngày ban hành </th>
      <th  style="text-align:center"> Trích yếu </th>
      <th style="text-align:center"> Nơi ban hành</th>
      <?php if(!(isset($_GET['ds'])&&$_GET['ds'] == 1))
		  {
		  ?>
      <th style="text-align:center"> Phòng ban xử lý</th>
      <th style="text-align:center"> <span class="glyphicon glyphicon-ok"></span> </th>
      <?php }?>
    </tr>
  </thead>
  <tbody>
    <?php
		
	if(!is_null($danhsach[0]['MaVanBanDen']))
	{
		
				for($i = 0; $i < count($danhsach); $i++)
				{
					$stt=$i+1;
					echo "<tr>";
						
						echo "<td>".$stt."</td>";
						
						if(isset($_POST['SovaKyHieu']))
							echo "<td> <a href='vanbanden-chitiet_{$danhsach[$i]['MaVanBanDen']}'>".highlight($danhsach[$i]['SovaKyHieu'], $SovaKyHieu)."</a></td>";
						else
							echo "<td> <a href='vanbanden-chitiet_{$danhsach[$i]['MaVanBanDen']}'>{$danhsach[$i]['SovaKyHieu']} </a></td>";
						
					
						
					
						if(isset($_POST['NgayBanHanh']))
							echo "<td>".highlight(InNgay($danhsach[$i]['NgayBanHanh']), str_replace(',','/',$NgayBanHanh) )."</td>";
						else
							echo "<td>".InNgay($danhsach[$i]['NgayBanHanh'])."</td>";
						
						if(isset($_POST['TrichYeu']))
							echo "<td>".highlight($danhsach[$i]['TrichYeu'],$TrichYeu)."</td>";
						else
							echo "<td>{$danhsach[$i]['TrichYeu']}</td>";
						
						if(isset($_POST['NoiBanHanh']))
							echo "<td>".highlight($danhsach[$i]['NoiBanHanh'],$NoiBanHanh)."</td>";
						else
							echo "<td>{$danhsach[$i]['NoiBanHanh']}</td>";
					
						if(!(isset($_GET['ds'])&&$_GET['ds'] == 1))
						{
						echo "<td>
							<select name='MaPhongXuLy[]'>";
							for($j=0;$j<count($dsphongban);$j++)
							{
								echo "<option value=".$dsphongban[$j]['MaPhongBan'].">{$dsphongban[$j]['TenPhongBan']}</option>";
							}
						echo"	</select>
						</td>";
						
						echo "<td><input type='checkbox' name='checkDuyet[]' value=".$danhsach[$i]['MaVanBanDen']." /></td>";
						}
					echo "</tr>";
				}
			}
			?>
    <?php if(!(isset($_GET['ds'])&&$_GET['ds'] == 1))
		  {
		  ?>
    <tr>
      <td colspan="9"><input value="Duyệt"  class="btn btn-primary btn-primary pull-right" name="btnDuyet" type="submit" /></td>
    </tr>
    <?php }?>
  </tbody>
</table>
<form>
</div>
<?php
		if($tongsodong > $limit)
		{
			echo "<div id='PhanTrang' align = 'center'>";
				echo "<ul class='pagination pagination-sm'>";
					if(isset($_GET['ds'])&&$_GET['ds'] == 1)
					{
						if($start > 0)
							echo "<li><a href='vanbanden-duyet-page-loai_$mtt-$back'>&laquo;</a></li>";
						$j = 1;
						for($i = 0; $i < $tongsodong; $i += $limit)
						{		
								echo "<li><a href='vanbanden-duyet-page-loai_$mtt-$i'>" . $j . "</a></li>";
							$j++;
						}
						if($start + $limit < $tongsodong)
							echo "<li><a href='vanbanden-duyet-page-loai_$mtt-$next'>&raquo;</a></li>";
					}else
					{
						if($start > 0)
							echo "<li><a href='vanbanden-duyet-page_$back'>&laquo;</a></li>";
						$j = 1;
						for($i = 0; $i < $tongsodong; $i += $limit)
						{		
								echo "<li><a href='vanbanden-duyet-page_$i'>" . $j . "</a></li>";
							$j++;
						}
						if($start + $limit < $tongsodong)
							echo "<li><a href='vanbanden-duyet-page_$next'>&raquo;</a></li>";
					}
				echo "</ul>";
			echo "</div>";
			echo "<br/>";
		}
?>
</div>
<script type="text/javascript">
   
	$('.form_date').datetimepicker({
      	format: 'dd-mm-yyyy',
        weekStart: 1,
        todayBtn:  1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 2,
		minView: 2,
		forceParse: 0
    });
	
</script>
<?php
	if(isset($_POST['btnDuyet']))
	{
		$vb = new vanbandenbs();
		$vb->MaNguoiDuyet = $_SESSION['MaNguoiDung'];
		$vb->MaTrangThai = 2;
		$MaVanBanDen = $_POST['checkDuyet'];
		$MaPhongXuLy = $_POST['MaPhongXuLy'];
		for($i=0; $i < count($MaVanBanDen); $i++)
		{
			$vb->MaVanBanDen = $MaVanBanDen[$i];
			$vb->MaPhongXuLy = $MaPhongXuLy[$i];
			$vb->ChangeTrangThai();
		}
		header("Location: vanbanden-duyet");
	}
}else
{
	ThongBaoLoi("Vui lòng đăng nhập !");
}
?>
